import Fastify from 'fastify';
import fastifyStatic from '@fastify/static';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { PORT } from './config/index.js'; 
import articleRoutes from './routes/articleRoutes.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const fastify = Fastify({
    logger: true,
});


fastify.register(fastifyStatic, {
    root: join(__dirname, 'public'),
    prefix: '/public/',
    index:false,
    list:true
});
fastify.register(articleRoutes, { prefix: '/api/article' });

fastify.get('/', async function handler(request, reply) {
    return { hello: 'world' };
});


try {
    await fastify.listen({ port: PORT,host:"0.0.0.0" });
} catch (err) {
    fastify.log.error(err);
    process.exit(1);
}

